package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallCollect;
import com.tencreat.service.mall.IMallCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 收藏Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="收藏api")
@RestController
@RequestMapping("/mall/collect")
public class AdminMallCollectController extends BaseController {
    @Autowired
    private IMallCollectService mallCollectService;

    /**
     * 查询收藏列表
     */
    @ApiOperation(value="查询收藏列表")
    @PreAuthorize("@ss.hasPermi('mall:collect:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallCollect>> list(@RequestBody  MallCollect mallCollect) {
        QueryWrapper<MallCollect> wrapper = Wrappers.query(mallCollect);
        Page<MallCollect> page =  new Page<>(mallCollect.getPageNum(), mallCollect.getPageSize());
        return HttpResult.ok(mallCollectService.page(page, wrapper));
    }

    /**
     * 导出收藏列表
     */
    @ApiOperation(value="导出收藏列表")
    @PreAuthorize("@ss.hasPermi('mall:collect:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallCollect mallCollect) {
        List<MallCollect> list = mallCollectService.selectMallCollectList(mallCollect);
        ExcelUtil<MallCollect> util = new ExcelUtil<MallCollect>(MallCollect.class);
        return util.exportExcel(list, "collect");
    }

    /**
     * 获取收藏详细信息
     */
    @ApiOperation(value="获取收藏详细信息")
    @PreAuthorize("@ss.hasPermi('mall:collect:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallCollect> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallCollectService.selectMallCollectById(id));
    }

    /**
     * 新增收藏
     */
    @ApiOperation(value="新增收藏")
    @PreAuthorize("@ss.hasPermi('mall:collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallCollect mallCollect) {
        return to(mallCollectService.insertMallCollect(mallCollect));
    }

    /**
     * 修改收藏
     */
    @ApiOperation(value="修改收藏")
    @PreAuthorize("@ss.hasPermi('mall:collect:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallCollect mallCollect) {
        return to(mallCollectService.updateMallCollect(mallCollect));
    }

    /**
     * 删除收藏
     */
    @ApiOperation(value="删除收藏")
    @PreAuthorize("@ss.hasPermi('mall:collect:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallCollectService.deleteMallCollectByIds(ids));
    }
}
